﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.IO;
namespace BlogDagangTool
{
    class Program
    {
        static void Main(string[] args)
        {
            //filePaths = System.IO.File.ReadAllLines(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)+"\\listfile.txt");
           
            string shtml ="";
            string skodekaos = "<div style=\"clear: both;\"></div><br/><br/><div class=\"kodekaos\"><h3 class=\"widgettitle\">[KDKAOS]</h3></div>";

            int[] ipola = { } ;
            bool bconst = false;
            int iconst = 0;
            string[] s = System.IO.File.ReadAllLines(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\src.txt");
            int i = 1;
            int c = 0;

            int currpola,  cPola = 0;
            currpola = 0;
            System.IO.StreamWriter sw = new System.IO.StreamWriter(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\jadinya.txt", false);
            int p = 0;
            string currkd = "";
            foreach (string ss in s)
            {
                #region Old Code
                //p += 1;
                //Console.WriteLine(p);
                //c += 1;
                //if (c == 1)
                //{
                //    if (ss.Trim().Substring(0, 1) == "0")
                //    {
                //        int.TryParse(ss.Substring(1, 1), out iconst);
                //        bconst = true;
                //    }
                //    else
                //    {
                //        char[] ch = ss.ToCharArray();
                //        ipola = new int[ch.Length];
                //        int k = 0;
                //        foreach (char ch2 in ch)
                //        {
                //            int.TryParse(ch2.ToString(), out ipola[k]);
                //            k += 1;
                //        }
                //        currpola = ipola[0];
                //        cPola = 0;
                //    }
                //}
                //else
                //{
                //    if (ss.Trim() != string.Empty && ss.Substring(0, 4) == "http")
                //    {
                //        string[] sparam = ss.Split('|');
                //        if (bconst)
                //        {

                //            string[] sk = System.IO.File.ReadAllLines(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\html.txt");
                //            shtml = sk[0];

                //            if (i == 1)
                //            {
                //                sw.WriteLine(skodekaos.Replace("[KDKAOS]", sparam[1]));
                //                htmlreplace(1, ref shtml, sparam);

                //            }
                //            else if (i == 2)
                //            {
                //                htmlreplace(2, ref shtml, sparam);
                //            }
                //            else if (i == 3)
                //            {
                //                htmlreplace(3, ref shtml, sparam);
                //            }
                //            else if (i == 4)
                //            {
                //                htmlreplace(4, ref shtml, sparam);
                //            }
                //            else if (i == 5)
                //            {
                //                htmlreplace(5, ref shtml, sparam);
                //            }


                //            i += 1;
                //            if (i == iconst + 1)
                //            {
                //                i = 1;
                //            }
                //            sw.WriteLine(shtml);
                //            sw.WriteLine();

                //        }
                //        else // kalo gak konstan
                //        {
                //            if (i == 1)
                //            {
                //                string[] sk = System.IO.File.ReadAllLines(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\html.txt");
                //                shtml = sk[iconst - 2];
                //                htmlreplace(1, ref shtml, sparam);

                //            }
                //            else if (i == 2)
                //            {
                //                htmlreplace(2, ref shtml, sparam);
                //            }
                //            else if (i == 3)
                //            {
                //                htmlreplace(3, ref shtml, sparam);
                //            }
                //            else if (i == 4)
                //            {
                //                htmlreplace(4, ref shtml, sparam);
                //            }
                //            else if (i == 5)
                //            {
                //                htmlreplace(5, ref shtml, sparam);
                //            }


                //            //i += 1;
                //            //if (i == currpola + 1)
                //            //{
                //            //    cPola += 1;
                //            //    currpola = ipola[cPola];
                //            //    i = 1;
                //            //    sw.WriteLine(shtml);
                //            //    sw.WriteLine();
                //            //}

                //            sw.WriteLine(shtml);


                //        }
                //    }
                //    else
                //    {
                //        sw.WriteLine();
                //    }
                //} 
                #endregion
                c++;

                string[] sk = System.IO.File.ReadAllLines(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\html.txt");
                shtml = sk[0];

                string simg = Path.GetFileName(ss);
                string skode;
                //simg = simg.Replace("-", "|");
                skode = (simg.Split('|'))[0];
                skode = (skode.Split('_'))[0];
                simg = simg.Replace("Light", "Light ");
                simg = simg.Replace("Misty", "Misty ");
                simg = simg.Replace(".jpg", "");
                //string[] stt = simg.Split('_');
                simg = (simg.Split('_'))[0];
                simg = ss+"|" + simg;


                if (skode != currkd)
                {
                    sw.WriteLine();
                    sw.WriteLine(skodekaos.Replace("[KDKAOS]", skode.Replace(".jpg", "")));
                    htmlreplace(1, ref shtml, simg.Split('|'));
                    currkd = skode;

                }
                else
                {
                    htmlreplace(1, ref shtml, simg.Split('|'));
                }

                Console.WriteLine(c.ToString());
                sw.WriteLine(shtml);







            }
            sw.Close();

            Console.WriteLine(shtml);





            Console.ReadKey();


           
        }

        private static void htmlreplace(int iplace, ref string shtml, string[] sparam)
        {
            try
            {

              
                    string[] sk = System.IO.File.ReadAllLines(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\html.txt");
                    shtml = sk[0];
                    //shtml = shtml.Replace("[KODEITEM]", sparam[1]);
                    shtml = shtml.Replace("[AHREF]", sparam[0]);
                    if (sparam.Length > 1) 
                    shtml = shtml.Replace("[ALT]", "Kaos " + sparam[1]);

                    string[] s = sparam[0].Split('/');






                
                    //shtml = shtml.Replace("[IMG]", sparam[0].Replace(sparam[1] + "-" + sparam[2], "th_" + sparam[1] + "-" + sparam[2]));
                    if (sparam.Length > 2)
                    {
                        shtml = shtml.Replace("[IMG]", sparam[0].Replace(s[s.Length - 1].ToString(), "th_" + s[s.Length - 1].ToString()));
                    }
                    else
                    {
                        shtml = shtml.Replace("[IMG]", sparam[0]);
                    }
                    if (sparam.Length > 2) 
                    shtml = shtml.Replace("[CLR]", sparam[2]);


              

            }
            catch (Exception ex)
            {

                throw ex;
            }

        }
        private static void htmlreplace1(int iplace,ref string shtml,string[] sparam)
        {
            try
            {

                if (iplace == 1)
                {
                    string[] sk = System.IO.File.ReadAllLines(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\html.txt");
                    shtml = sk[0];
                    shtml = shtml.Replace("[KODEITEM]", sparam[1]);
                    shtml = shtml.Replace("[AHREF1]", sparam[0]);
                    shtml = shtml.Replace("[ALT1]", "Kaos " + sparam[1]);
                    shtml = shtml.Replace("[IMG1]", sparam[0].Replace(sparam[1]+"-"+sparam[2],"th_"+sparam[1]+"-"+sparam[2]));
                    shtml = shtml.Replace("[CLR1]", sparam[2]);


                }
                else if (iplace == 2)
                {
                    shtml = shtml.Replace("[AHREF2]", sparam[0]);
                    shtml = shtml.Replace("[ALT2]", "Kaos " + sparam[1]);
                    shtml = shtml.Replace("[IMG2]", sparam[0].Replace(sparam[1]+"-"+sparam[2],"th_"+sparam[1]+"-"+sparam[2]));
                    shtml = shtml.Replace("[CLR2]", sparam[2]);
                }
                else if (iplace == 3)
                {
                    shtml = shtml.Replace("[AHREF3]", sparam[0]);
                    shtml = shtml.Replace("[ALT3]", "Kaos " + sparam[1]);
                    shtml = shtml.Replace("[IMG3]", sparam[0].Replace(sparam[1]+"-"+sparam[2],"th_"+sparam[1]+"-"+sparam[2]));
                    shtml = shtml.Replace("[CLR3]", sparam[2]);
                    //sw.WriteLine(shtml);
                    //sw.WriteLine();
                }
                else if (iplace == 4)
                {
                    shtml = shtml.Replace("[AHREF4]", sparam[0]);
                    shtml = shtml.Replace("[ALT4]", "Kaos " + sparam[1]);
                    shtml = shtml.Replace("[IMG4]", sparam[0].Replace(sparam[1]+"-"+sparam[2],"th_"+sparam[1]+"-"+sparam[2]));
                    shtml = shtml.Replace("[CLR4]", sparam[2]);
                }
                else if (iplace == 5)
                {
                    shtml = shtml.Replace("[AHREF5]", sparam[0]);
                    shtml = shtml.Replace("[ALT5]", "Kaos " + sparam[1]);
                    shtml = shtml.Replace("[IMG5]", sparam[0].Replace(sparam[1] + "-" + sparam[2], "th_" + sparam[1] + "-" + sparam[2]));
                    shtml = shtml.Replace("[CLR5]", sparam[2]);
                }

            }
            catch (Exception ex)
            {

                throw ex;
            }

        }
    }
}
